package com.lkd.controller;


import com.lkd.entity.Partner;
import com.lkd.service.PartnerService;
import com.lkd.vo.PageBean;
import com.lkd.vo.PartnerVo;
import com.lkd.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 合作商表 前端控制器
 * </p>
 *
 * @author 聂德祥
 * @since 2024-08-19
 */
@RestController
public class PartnerController {
    @Autowired
    private PartnerService partnerService;
//    添加合作商
@PostMapping("/api/user-service/partner")
    public Result addPartner(@RequestBody Partner partner){
    partner.setStatus(1);
    // TODO 密码加密
    partnerService.save(partner);
    return Result.success();
}
// 分页查询合作商
@GetMapping("/api/user-service/partner/search")
    public Result searchPartnerByPage(String name,@RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "10") Integer pageSize){
 PageBean<PartnerVo> page = partnerService.findByPage(name,pageIndex,pageSize);
    return Result.success(page);
}
// 根据id查询合作商
@GetMapping("/api/user-service/partner/{id}")
    public Result getPartnerById(@PathVariable Integer id){
    Partner partner = partnerService.getPartnerById(id);
    return Result.success(partner);
}
// 修改合作商
@PutMapping("/api/user-service/partner")
    public Result updatePartner(@RequestBody Partner partner){
    partnerService.updateById(partner);
    return Result.success();
}
// 删除合作商
    @DeleteMapping("/api/user-service/partner/{id}")
    public Result deletePartner(@PathVariable Integer id){
    // TODO 删除合作商之前，判断该合作商是否已经绑定了售货机
        partnerService.removeById(id);
        return Result.success();
    }
    @GetMapping("/api/user-service/partner/count")
    public Result getCount(){
        Integer count = partnerService.getCount();
        return Result.success(count);
    }
}
